<?php

namespace App\Http\Controllers;

use App\AboutModel;
use App\ImageModel;
use App\NewsModel;
use App\ProModel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Storage;

class AjaxController extends Controller
{
    //
    public function news(Request $request, $id)
    {
        if ($request->ajax()) {
            $news_json = NewsModel::get($id);
            return $news_json;
        }
        return false;
    }

    //
    public function pro(Request $request, $id)
    {
        if ($request->ajax()) {
            $alone_json = ProModel::get_alone($id);
            $image_json = ProModel::get_img($id);
            $res_json = json_encode([json_decode($alone_json), json_decode($image_json)]);
            return $res_json;
        }
        return false;
    }

    //
    public function proItem(Request $request, $id)
    {
        if ($request->ajax()) {
            $alone_item_json = ProModel::get_item_alone($id);
            return $alone_item_json;
        }
        return false;
    }

    public function upload_wx(Request $request)
    {
        if ($request->ajax()) {
            $wx_code = $request->file('upload-wx');
            if ($wx_code->isValid()) { //括号里面的是必须加的哦
                //如果括号里面的不加上的话，下面的方法也无法调用的

                //获取文件的扩展名
                $ext = $wx_code->getClientOriginalExtension();

                //获取文件的绝对路径
                $path = $wx_code->getRealPath();

                //定义文件名
                $filename = md5(time() . Auth::id()) . '.' . $ext;

                //存储文件。disk里面的public。总的来说，就是调用disk模块里的public配置
                Storage::disk('uploads')->put($filename, file_get_contents($path));
                return [
                    'status' => 1,
                    'filename' => $filename
                ];
            }
        }
        return ['status' => 0];
    }

    public function upload_avatar(Request $request)
    {
        if ($request->ajax()) {
            $wx_code = $request->file('upload-avatar');
            if ($wx_code->isValid()) { //括号里面的是必须加的哦
                //如果括号里面的不加上的话，下面的方法也无法调用的

                //获取文件的扩展名
                $ext = $wx_code->getClientOriginalExtension();

                //获取文件的绝对路径
                $path = $wx_code->getRealPath();

                //定义文件名
                $filename = md5(time() . Auth::id()) . '.' . $ext;

                //存储文件。disk里面的public。总的来说，就是调用disk模块里的public配置
                Storage::disk('uploads')->put($filename, file_get_contents($path));
                return [
                    'status' => 0,
                    'filename' => $filename
                ];
            }
            return ['status' => 1];
        }
        return ['status' => 2];
    }

    public function upload_swiper(Request $request)
    {
        if ($request->ajax()) {
            $wx_code = $request->file('upload-swiper');
            if ($wx_code->isValid()) { //括号里面的是必须加的哦
                //如果括号里面的不加上的话，下面的方法也无法调用的

                //获取文件的扩展名
                $ext = $wx_code->getClientOriginalExtension();

                //获取文件的绝对路径
                $path = $wx_code->getRealPath();

                //定义文件名
                $filename = md5(time() . Auth::id()) . '.' . $ext;

                //存储文件。disk里面的public。总的来说，就是调用disk模块里的public配置
                Storage::disk('uploads')->put($filename, file_get_contents($path));
                return [
                    'status' => 0,
                    'filename' => $filename
                ];
            }
            return ['status' => 1];
        }
        return ['status' => 2];
    }

    public function update_ao(Request $request)
    {
        if ($request->ajax()) {
            $update_array = [
                '公司名称' => $request->post('com_name'),
                '公司介绍' => $request->post('com_dede'),
                '公司电话' => $request->post('com_tel'),
                '公司传真' => $request->post('com_fax'),
                '公司地址' => $request->post('com_add'),
                '微信二维码' => $request->post('com_wx'),
                '备案' => $request->post('com_ipc'),
                '版权' => $request->post('com_cpr'),
                '公司LOGO' => $request->post('com_logo'),
                '右上角电话' => $request->post('com_rtt')
            ];
            $result = AboutModel::update_about_other($update_array);
            if ($result === 0)
                return ['status' => 0];
            else
                return ['status' => 1];
        }
        return ['status' => 2];
    }

    public function update_us(Request $request, $id)
    {
        if ($request->ajax()) {
            $update_array = [
                'name' => $request->post('us-name'),
                'job' => $request->post('us-job'),
                'avatar' => $request->post('us-avatar')
            ];
            $updated = $result = AboutModel::update_about_us($id, $update_array);
            if ($updated === 0) {
                return ['status' => 0, 'alert' => '修改成功', 'data' => $update_array];
            } elseif ($updated === 1) {
                AboutModel::add_about_us($id, $update_array);
                return ['status' => 1, 'alert' => '添加成功', 'data' => $update_array];
            }
        }
        return ['status' => 2];
    }

    public function delete_us(Request $request, $id)
    {
        if ($request->ajax()) {
            return ['status' => AboutModel::delete_about_us($id)];
        }
        return ['status' => 2];
    }

    public function delete_news(Request $request)
    {
        if ($request->ajax()) {
            $id_list = $request->post('id_list');
            $id_count = count($id_list);
            for ($i = 0; $i < $id_count; $i++) {
                if (!NewsModel::delete_id($id_list[$i])) {
                    return ['status' => 1];
                }
            }
            return ['status' => 0];
        }
        return ['status' => 2];
    }

    public function upload_new_show_img(Request $request)
    {
        if ($request->ajax()) {
            $wx_code = $request->file('upload-new-show-img');
            if ($wx_code->isValid()) { //括号里面的是必须加的哦
                //如果括号里面的不加上的话，下面的方法也无法调用的

                //获取文件的扩展名
                $ext = $wx_code->getClientOriginalExtension();

                //获取文件的绝对路径
                $path = $wx_code->getRealPath();

                //定义文件名
                $filename = md5(time() . Auth::id()) . '.' . $ext;

                //存储文件。disk里面的public。总的来说，就是调用disk模块里的public配置
                Storage::disk('uploads')->put($filename, file_get_contents($path));
                return [
                    'status' => 0,
                    'filename' => $filename
                ];
            }
            return ['status' => 1];
        }
        return ['status' => 2];
    }

    public function upload_pro_show_img(Request $request)
    {
        if ($request->ajax()) {
            $wx_code = $request->file('upload-pro-show-img');
            if ($wx_code->isValid()) { //括号里面的是必须加的哦
                //如果括号里面的不加上的话，下面的方法也无法调用的

                //获取文件的扩展名
                $ext = $wx_code->getClientOriginalExtension();

                //获取文件的绝对路径
                $path = $wx_code->getRealPath();

                //定义文件名
                $filename = md5(time() . Auth::id()) . '.' . $ext;

                //存储文件。disk里面的public。总的来说，就是调用disk模块里的public配置
                Storage::disk('uploads')->put($filename, file_get_contents($path));
                return [
                    'status' => 0,
                    'filename' => $filename
                ];
            }
            return ['status' => 1];
        }
        return ['status' => 2];
    }

    public function upload_new_content_img(Request $request)
    {
        if ($request->ajax()) {
            $wx_code = $request->file('upload-new-content-img');
            if ($wx_code->isValid()) { //括号里面的是必须加的哦
                //如果括号里面的不加上的话，下面的方法也无法调用的

                //获取文件的扩展名
                $ext = $wx_code->getClientOriginalExtension();

                //获取文件的绝对路径
                $path = $wx_code->getRealPath();

                //定义文件名
                $filename = md5(time() . Auth::id()) . '.' . $ext;

                //存储文件。disk里面的public。总的来说，就是调用disk模块里的public配置
                Storage::disk('uploads')->put($filename, file_get_contents($path));
                return [
                    'status' => 0,
                    'filename' => $filename
                ];
            }
            return ['status' => 1];
        }
        return ['status' => 2];
    }

    public function update_news(Request $request)
    {
        if ($request->ajax()) {
            $request_array = [
                'id' => $request->post('new-id'),
                'show' => $request->post('new-show'),
                'title' => $request->post('new-title'),
                'micro' => $request->post('new-micro'),
                'content' => $request->post('new-content')
            ];
            if (!NewsModel::update_new($request_array)) {
                NewsModel::add($request_array);
                return ['status' => 0, 'data' => $request_array];
            }
            return ['status' => 1, 'data' => $request_array];
        }
        return ['status' => 2];
    }

    // pro删除
    public function delete_pros(Request $request)
    {
        if ($request->ajax()) {
            $id_list = $request->post('id_list');
            $id_count = count($id_list);
            for ($i = 0; $i < $id_count; $i++) {
                if (!ProModel::delete_id($id_list[$i])) {
                    return ['status' => 1];
                }
            }
            return ['status' => 0];
        }
        return ['status' => 2];
    }

    public function get_pro(Request $request, $id)
    {
        if ($request->ajax()) {
            $alone_get = ProModel::get_alone($id);
            $img_get = ProModel::get_img($id);
            return ['status' => 0, 'data' => $alone_get, 'image' => $img_get];
        }
        return ['status' => 2];
    }

    public function get_new(Request $request, $id)
    {
        if ($request->ajax()) {
            $news_get = NewsModel::get($id);
            return ['status' => 0, 'data' => $news_get];
        }
        return ['status' => 2];
    }

    public function update_pro(Request $request)
    {
        if ($request->ajax()) {
            $id = $request->post('pro-id');
            $request_array = [
                'title' => $request->post('pro-title'),
                'show_img' => $request->post('pro-show-img'),
                'type' => $request->post('pro-type'),
                'material' => $request->post('pro-material'),
                'size' => $request->post('pro-size'),
                'color' => $request->post('pro-color'),
                'bag_type' => $request->post('pro-bag-type'),
                'de_de' => $request->post('pro-de-de'),
                'item_id' => $request->post('pro-item-id'),
                'zh_type' => $request->post('pro-zh-type'),
                'updated_at' => date('Y-m-d h:i:s')
            ];
            if (!ProModel::update_pro($id, $request_array)) {
                if (ProModel::insert_pro($id, $request_array)) {
                    return ['status' => 0, 'data' => ProModel::get_alone($id)];
                } else {
                    return ['status' => 3];
                }
            }
            return ['status' => 1, 'data' => ProModel::get_alone($id)];
        }
        return ['status' => 2];
    }

    // swiper
    public function update_swiper(Request $request)
    {
        if ($request->ajax()) {
            $id = $request->post('swiper-id');
            $request_array = [
                'alt' => $request->post('swiper-alt'),
                'filename' => $request->post('swiper-filename'),
                'go' => $request->post('swiper-go')
            ];
            if (!ImageModel::swiper_update($id, $request_array)) {
                ImageModel::swiper_add($id, $request_array);
                return ['status' => 0, 'data' => $request_array];
            }
            return ['status' => 1, 'data' => $request_array];
        }
        return ['status' => 2];
    }
}










